<?php
namespace App\Admin\Renderable\Mp;

use App\Models\Mp\WxMpMessage;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class MpMessageRender extends LazyRenderable
{
    protected $mp_message_types = [
        'text' => '文本',
        'image' => '图片',
        'voice' => '语音',
        'video' => '视频',
        'music' => '音乐',
        'news' => '外链图文',
        'mpnews' => '公众号图文',
        'wxcard' => '卡卷消息'
    ];
    protected $mp_message_type_colors = [
        'text' => '#3498db',
        'image' => '#e74c3c',
        'voice' => '#2ecc71',
        'video' => '#f1c40f',
        'music' => '#9b59b6',
        'news' => '#e67e22',
        'mpnews' => '#1abc9c',
        'wxcard' => '#34495e'
    ];

    public function grid(): Grid
    {
        return Grid::make(new WxMpMessage(), function (Grid $grid) {
            $grid->model()->whereNotIn('msgtype',['material_image', 'material_video', 'material_voice', 'material_music'])->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('msgtype', '类型')->using($this->mp_message_types)->label($this->mp_message_type_colors);
            $grid->column('content', '文字');
            $grid->column('img_url', '图片')->image('', 100, 100);
            $grid->column('video_url', '视频')->display(function ($v){
                if($v){
                    return '<video controls width="150">
  <source src="'.$v.'" type="video/mp4">
  您的浏览器不支持 video 标签。
</video>';
                }
                return '';
            });
            $grid->column('voice_url', '语音')->display(function ($v){
                if($v){
                    return '<audio controls><source src="'.$v.'" type="audio/mp3"></audio>';
                }
                return '';
            });
//            $grid->column('thumb_url', '缩略图')->image('', 100, 100);
            $grid->column('musicurl', '音乐')->display(function ($v){
                if($v){
                    return '<audio controls><source src="'.$v.'" type="audio/mp3"></audio>';
                }
                return '';
            });
//            $grid->column('media_id')->display();
//            $grid->column('thumb_media_id');
            $grid->column('title', '标题');
//            $grid->column('description');
//            $grid->column('hqmusicurl');
            $grid->column('url')->display(function ($v){
                if($v){
                    return '<a href="'.$v.'" target="_blank">链接</a>';
                }
                return '';
            });
//            $grid->column('article_id');
//            $grid->column('card_id');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }
}
